from models import Language

def getlang(deployId,key):
    try :
        return Language.objects.get(key=key,deployId=deployId)
    except:
        return None

def getlangById(langId):
    try :
        return Language.objects.get(id=langId)
    except:
        return None

def setlang(type,deployId,key,value):
    lang = getlang(key,deployId)
    if lang:
        lang.value = value
    else:
        lang = Language()
        lang.type = type
        lang.deployId = deployId
        lang.key = key
        lang.value = value
    lang.save()
    return lang

def setlangById(langId,value):
    lang = getlangById(langId)
    if lang:
        lang.value = value
        lang.save()

def dellangById(langId):
    try:
        lang = Language.objects.get(id = langId)
        lang.delete()
    except:
        pass

def dellang(deployId,key=None):
    try:
        if not key :
            langs = Language.objects.filter(deployId = deployId)
            for lang in langs:
                lang.delete()
        else:
            lang = Language.objects.get(deployId = deployId,key = key)
            lang.delete()
    except:
        pass